import axios from "axios";
import { ElMessage } from 'element-plus';
import 'element-plus/theme-chalk/el-loading.css';
import 'element-plus/theme-chalk/el-message.css';

//  axios.defaults.baseURL = "http://172.16.129.45:8081/admin";
axios.defaults.baseURL = "http://172.16.129.79:8081/admin";
// axios.defaults.baseURL = "http://172.16.129.235:8080/admin";

axios.interceptors.request.use(
  function (config) {
    // Do something before request is sent
    let token: any = sessionStorage.getItem("token");
    // let arrToken = token.split(".");
    // let str = arrToken[1];
    // let user = JSON.parse(decodeURIComponent(escape(window.atob(str))));
    // console.log(str,user);
    //Authorization或者是power
    //如果有power并且对应
    // token &&console.log(JSON.parse(JSON.parse(token))[0]);
    // token && (JSON.parse(JSON.parse(token))[0]=token);
    config.headers["token"] = token;
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
axios.interceptors.response.use(
  function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    //console.log('res1',response);
    switch (response.data.code) {
      case 20003:
        ElMessage({
          type: 'error',
          message: response.data.msg
      })
        sessionStorage.clear();
        window.location.href = "http://localhost:5173/";
        case 20006:
          ElMessage({
            type: 'error',
            message: response.data.msg
        })
        window.location.href = "http://localhost:5173/";
    }
    return response;
  },
  function (error) {
    //console.log('res2',error)
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  }
);
export default axios;
